import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Creater with IntelliJ IDEA.
 * Description:
 * User: 86152
 * Dae: 2022-07-06
 * Time: 21:19
 */
public class Test {

    public static void main(String[] args) {
        int[] stones = {2,7,4,1,8,1};
        Solution solution = new Solution();
        solution.lastStoneWeight(stones);
    }
}
class Solution {
    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        for(int i=0;i<stones.length;i++) {
            priorityQueue.offer(stones[i]);
        }
        while(priorityQueue.size()>1) {
            int a=priorityQueue.poll();
            int b=priorityQueue.poll();
            if(a!=b) {
                priorityQueue.offer(a-b);
            }
        }
        return priorityQueue.isEmpty()?0:priorityQueue.poll();
    }
}
